#
#  Ships.py
#  TurnShip
#
#  Created by Pete Lord on 22/12/2008.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
import math, Options, Objects, os, pygame, random, Effects
        
class Ships(dict):
    def __setitem__(self, i, y):
        y.__setup__(self.entities)
        dict.__setitem__(self, i, y)
        
    def __setup__(self, entities, player):
        self.entities = entities
        for ship in self.values():
            ship.__setup__(self.entities, player)
    
    def get_key(self, item):
        for key, element in self.items():
            if element == item:
                return key
        raise KeyError

class Ship(Objects.Object):
    def __setup__(self, entities):
        Objects.Object.__init__(self, entities, self.x, self.y, self.r)
        self.selected = False
        for weapon in self.weapons:
            weapon.__setup__(self, entities.ship_graphics)
    
    def draw_lights(self):
        return self.textures[Options.ZOOM_LEVEL][1][self.r][self.current_frame], pygame.Rect(self.screen_location(), (Options.SPRITE_SIZE[Options.ZOOM_LEVEL], Options.SPRITE_SIZE[Options.ZOOM_LEVEL]))

    def turn(self, amount):
        self.r += amount
                
    def select(self):
        self.selected = True
        
    def deselect(self):
        self.selected = False
        
    def key(self):
        return self.__key__
        
    def hit(self):
        self.entities.effects.add_higher(Effects.ShipHit(self, self.entities))
        
    def hit_critical(self):
        self.entities.effects.add_higher(Effects.ShipHitCritical(self, self.entities))
    
    def die(self):
        self.entities.effects.add_higher(Effects.ShipDeath(self, self.entities))

class Pawn(Ship):
    def __setup__(self, entities, player):
        Ship.__setup__(self, entities)
        # animation data goes here. Do we have animation? how many frames? a list of frame timings please.
        self.has_anim = True
        self.anim_frames = 2
        self.anim_frame_times = 10 # 
        self.current_frame = 0 # init.
        self.current_time = random.randint(0, self.anim_frame_times)
        
        if player <= 4:
            self.textures = entities.ship_graphics[os.path.join('Data', 'Ships', 'PawnP' + str(player + 1))] # graphics.
        else:
            self.textures = entities.ship_graphics[os.path.join('Data', 'Ships', 'TestShip')] # graphics.
            
    def fire(self, weapon):
        self.xy_mod.push(self.weapons[weapon].recoil, self.r + self.weapons[weapon].recoil_direction)
        self.entities.effects.add(Effects.PawnCannonA(self, self.entities, self.weapons[weapon].textures))
        
class MissingNo(Ship):
    def __setup__(self, entities):
        self.health = 10 # set by class.
        Ship.__setup__(self, entities)
        # animation data goes here. Do we have animation? how many frames? a list of frame timings please.
        self.has_anim = False
        self.anim_frames = 0
        self.anim_frame_times = None
        
        self.weapons = [] # needs to have a testgun added. Like. Not all ships nessecarily have gunz.
        self.textures = entities.ship_graphics[os.path.join('Data', 'Ships', 'MissingNo')] # graphics.
